import 'package:dio/dio.dart';
import 'package:get/get.dart';

import '../../routes/app_pages.dart';
import '../../services/auth_service.dart';
import '../../utils/log_utils.dart';
import '../../utils/toast_utils.dart';
import '../data_helper.dart';


/// @author zavier
/// @time 2022/6/27 14:53
/// @des http拦截器，添加请求头和通用参数

class HeaderInterceptor extends InterceptorsWrapper {
  @override
  void onRequest(RequestOptions options, RequestInterceptorHandler handler) async {
    options.headers.addAll(DataHelper.getDefaultHeader());
    super.onRequest(options, handler);
  }
  @override
  void onError(DioError err, ErrorInterceptorHandler handler) {
    super.onError(err, handler);
    if (err.response?.statusCode == 302) {
      ToastUtils.show("您的登录态已经过过期,请重新登录");
      Get.find<AuthService>().logout();
      Get.offAllNamed(Routes.login);
      LogUtils.d("登录过期了");
    }
  }
}
